package com.icangya.admin;

import com.icangya.Utils.OAuthAccessToken;
import com.icangya.Utils.WeixinUtil;
import com.icangya.Value.StaticValue;
import com.icangya.service.IAdminUserService;
import com.icangya.service.IDomainService;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;


/**
 * 测试
 * @authorAngel(QQ:412887952)
 * @version v.0.1
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/CenterServerController")
public class CenterServerController {

    @Autowired
    private IDomainService iDomainService;

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    private WeixinUtil weixinUtil = WeixinUtil.getIstance();

    @Autowired
    private IAdminUserService iAdminUserService;


    @RequestMapping("/erweimaLogin1")
    public void erweimaLogin(Long parentID,Long proxyID,HttpServletResponse response){


        if(iAdminUserService.getSwitch().getStatus()==0){
            try {
                response.sendRedirect("http://www.gentaiyuan.cn/sys/wh.html");
                return ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String shareUrl = null;
        try {
            shareUrl = StaticValue.WX.ShareUrl.replace("APPID", StaticValue.WX.AppId).replace("REDIRECT_URI",
                    URLEncoder.encode(StaticValue.WX.CENTER_URL + "parentID=" + parentID + "&proxyID=" + proxyID,"UTF-8"))
                    .replace("SCOPE", StaticValue.WX.HAVE_USER_INFO_SCOPE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            response.sendRedirect(shareUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/RedirectGameUrl")
    public void RedirectGameUrl(@RequestParam String code, @RequestParam String state, Long parentID, Long proxyID, HttpServletResponse response){

        String shareUrl = null;

        if (parentID == null)
            parentID = 0L;//若无则是系统
        if (proxyID == null)
            proxyID = 1L;//若无则是admin

        try {

            logger.info("appid:"+ StaticValue.WX.AppId+"===appsecret:"+ StaticValue.WX.AppSecret);
            OAuthAccessToken accessToken = weixinUtil.getOAuthAccessToken(StaticValue.WX.AppId, StaticValue.WX.AppSecret, code);

            accessToken = weixinUtil.refershOAuthAccessToken(StaticValue.WX.AppId, accessToken.getRefreshToken());//刷新accessTokenn
            String openID = accessToken.getOpenid();

            org.json.JSONObject userjson = weixinUtil.acceptOAuthUserNews(accessToken.getAccessToken(),openID);

            shareUrl = "http://"+iDomainService.getDoamin().get(0).getDomainUrl()+"/wechat/testloginCallBack?openID=OPENID&nickname=NICKNAME&headimgurl=HEADIMGURL&parentID=PARENTID&proxyID=PROXYID";

            String nickname = URLEncoder.encode(userjson.getString("nickname"),"utf-8");
            logger.info(nickname+"========================RedirectGameUrl======================");
            try {
                shareUrl = shareUrl.replace("OPENID",openID)
                        .replace("NICKNAME",nickname)
                        .replace("HEADIMGURL",userjson.getString("headimgurl"))
                        .replace("PARENTID",parentID.toString()).replace("PROXYID",proxyID.toString());
                logger.info("shareUrl : " + shareUrl);

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            response.sendRedirect(shareUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
